//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || [];
    logs.unshift(Date.now());
    wx.setStorageSync('logs', logs)
    // console.log("lohs")
    // console.log(logs)
  },
  getUserInfo:function(cb){
    var get_user=wx.getSystemInfoSync();
    // console.log(get_user.SDKVersion.replace(/\./g,""));
    // 判断微信版本
    if (get_user.SDKVersion.replace(/\./g, "")<120){
      wx.showModal({
        title:'微信版本过低',
        content:"微信版本过低请升级"
      })
      return
    }
    // if(get_user.SDKVersion)
    // console.log("user)info")
    // console.log(this.globalData.userInfo)
    var that = this;
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      // console.log(this.globalData.userInfo)
      //调用登录接口
      wx.login({
        success: function (e) {
               let code = e.code;
              //  console.log(code)
               wx.login({
                 success: function (rst) {
                   wx.getUserInfo({
                     success: function (res) {
                      //  console.log("succc")
                       that.globalData.userInfo = res.userInfo
                       typeof cb == "function" && cb(that.globalData.userInfo)
                      //  console.log(res.userInfo.avatarUrl)
                       wx.getWeRunData({
                         success: function (et) {
                          //  console.log("sssss")
                           wx.request({
                             url: 'https://woxiaoshitang.com/activity/health.php/applet/index',
                             method: "POST",
                             data: {
                               "iv": et.iv,
                               "code": rst.code,
                               "encryptedData": et.encryptedData,
                               'nickname': res.userInfo.nickName,
                               "avatarUrl": res.userInfo.avatarUrl
                             },
                             header: {
                               'content-type': 'application/x-www-form-urlencoded'
                             },
                             success: function (res) {
                              //  console.log(res.data)
                              //  console.log(eval(res.data))
                               //console.log(eval("(" + res.data + ")")) 
                              //  console.log(res.data)
                               let json = res.data.trim();
                               let arr = JSON.parse(json);
                               console.log(arr)
                               try {
                                 wx.setStorageSync('time', arr.time);
                                 wx.setStorageSync('stepnumber', arr.stepnumber);
                                 wx.setStorageSync('openid', arr.openid);
                                 wx.setStorageSync('userid', arr.userid);
                                 console.log("opendi_111111_+__+_+_+_+_+")
                                 console.log(arr.openid)
                                //  wx.setStorageSync('nickname', res.userInfo.nickName);
                               } catch (e) {
                                 console.log(e)
                               }
                               if(arr.message == 0){
                                 wx.redirectTo({
                                   url: '../get_mess/get_mess',
                                 })
                               }else{
                                 wx.redirectTo({
                                   url: '../healthy/healthy',
                                 })
                               }
                               //if(res.message)
                               
                             }
                           })
                         },fail:function(e){
                                   console.log(e)
                         }
                       });
                     }
                   })
                 }
               })
        }
      })
    }
  },
  globalData:{
    userInfo:null
  }
})